package com.zgm.ch05.aop.after._3_5_4_2;

import org.springframework.aop.framework.ProxyFactory;

public class AfterAdviceExample {
	
	public static void main(String[] args) {
		KeyGenerator keyGen = getKeyGenerator();
		
		for (int i = 0; i < 10; i++) {
			try {
				long key = keyGen.getKey();
				System.out.println("key: " + key);
			} catch(SecurityException ex) {
				System.out.println("生成了弱密钥:" + ex.getMessage());
			}
			
//			long key = keyGen.getKey();
//			System.out.println("key: " + key);
		}
		
	}
	
	private static KeyGenerator getKeyGenerator() {
		KeyGenerator target = new KeyGenerator();
		ProxyFactory pf = new ProxyFactory();
		
		pf.setTarget(target);
		pf.addAdvice(new WeakKeyCheckAdvice());
		
		KeyGenerator proxy = (KeyGenerator) pf.getProxy();
		
		return proxy;
	}

}
